package com.cskaoyan.service.impl;

import com.cskaoyan.bean.bo.CategoryCreateBO;
import com.cskaoyan.bean.bo.CategoryDeleteBO;
import com.cskaoyan.bean.pojo.MarketAddress;
import com.cskaoyan.bean.pojo.MarketAddressExample;
import com.cskaoyan.bean.pojo.MarketCategory;
import com.cskaoyan.bean.pojo.MarketCategoryExample;
import com.cskaoyan.bean.vo.CategoryCreateVO;
import com.cskaoyan.bean.vo.CategoryL1InnerIdAndName;
import com.cskaoyan.bean.vo.CategoryL1VO;
import com.cskaoyan.bean.vo.CategoryListVo;
import com.cskaoyan.mapper.MarketCategoryMapper;
import com.cskaoyan.service.CategoryService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;

/**
 * @author rebaogu
 * @description 商品目录实现类
 * @date 2022.04.06 20:00
 * @package com.cskaoyan.service
 * @Classname CategoryServiceImpl
 */
@Service
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    MarketCategoryMapper marketCategoryMapper;

    @Override
    public CategoryListVo categoryList() {

        // 拼接排序依据：sort和order
        MarketCategoryExample marketCategoryExample = new MarketCategoryExample();

        // example.setOrderByClause(sort + " " + order);

        // 执行查询
        List<MarketCategory> marketCategories = marketCategoryMapper.selectByExample(marketCategoryExample);

        ArrayList<MarketCategory> marketCategoriesFirst = new ArrayList<>();

        // 获取一级目录
        for (MarketCategory marketCategory : marketCategories) {
            String level = marketCategory.getLevel();
            Boolean deleted = marketCategory.getDeleted();
            if (level != null && deleted != null) {
                if ("L1".equals(level)) {
                    if (!deleted) {
                        marketCategoriesFirst.add(marketCategory);
                    }
                }
            }

        }
        // 获取查询结果的分页相关信息
        PageInfo<MarketAddress> pageInfo = new PageInfo<>();
        long total = pageInfo.getTotal();
        int pages = pageInfo.getPages();

        CategoryListVo categoryListVo = new CategoryListVo();

        categoryListVo.setTotal((int) total);
        categoryListVo.setPages(pages);
        categoryListVo.setLimit(15);
        categoryListVo.setPage(1);
        // 定义一个内部类数组
        ArrayList<CategoryListVo.ListDTO> listDTOS = new ArrayList<>();

        // 一级目录遍历
        for (MarketCategory mCF : marketCategoriesFirst) {
            // 根据一级目录的条件查询到二级目录,封装进一级目录对象
            // 获取 id
            Integer id = mCF.getId();
            MarketCategoryExample example = new MarketCategoryExample();

            MarketCategoryExample.Criteria criteria = example.createCriteria();
            // 查询pid为一级目录id的
            criteria.andPidEqualTo(id).andDeletedEqualTo(false);

            List<MarketCategory> marketCategoriesSecond = marketCategoryMapper.selectByExample(example);

            CategoryListVo.ListDTO listDTO = new CategoryListVo.ListDTO(
                    mCF.getId(), mCF.getName(), mCF.getKeywords()
                    , mCF.getDesc(), mCF.getIconUrl(), mCF.getPicUrl(), mCF.getLevel()
                    , marketCategoriesSecond
            );
            listDTOS.add(listDTO);
        }
        categoryListVo.setList(listDTOS);
        return categoryListVo;
    }


    @Override
    public CategoryCreateVO categoryCreate(CategoryCreateBO categoryCreateBO) {
        // 1, 获取--> 插入
        MarketCategory marketCategory = new MarketCategory();
        marketCategory.setName(categoryCreateBO.getName());
        marketCategory.setKeywords(categoryCreateBO.getKeywords());
        marketCategory.setLevel(categoryCreateBO.getLevel());
        marketCategory.setPid(categoryCreateBO.getPid());
        marketCategory.setDesc(categoryCreateBO.getDesc());
        marketCategory.setIconUrl(categoryCreateBO.getIconUrl());
        marketCategory.setPicUrl(categoryCreateBO.getPicUrl());
        marketCategory.setAddTime(new Date());
        marketCategory.setUpdateTime(new Date());
        marketCategory.setDeleted(false);

        MarketCategoryExample example = new MarketCategoryExample();

        MarketCategoryExample.Criteria criteria = example.createCriteria();
        criteria.andNameEqualTo(categoryCreateBO.getName());

        long count = marketCategoryMapper.countByExample(example);

        // 如果行数为0 --> 没有相同名字的人 --> 添加
        if (count == 0) {
            marketCategoryMapper.insert(marketCategory);
        }

        // 3, 查询对应条件 --> 放入 categoryCreateVO
        MarketCategoryExample example2Select = new MarketCategoryExample();

        MarketCategoryExample.Criteria criteria2Select = example.createCriteria();

        criteria2Select.andNameEqualTo(categoryCreateBO.getName())
                .andKeywordsEqualTo(categoryCreateBO.getKeywords())
                .andLevelEqualTo(categoryCreateBO.getLevel())
                .andPidEqualTo(categoryCreateBO.getPid())
                .andDescEqualTo(categoryCreateBO.getDesc());

        List<MarketCategory> marketCategories = marketCategoryMapper.selectByExample(example2Select);


        CategoryCreateVO categoryCreateVO = null;
        for (MarketCategory marketCategory2Select : marketCategories) {
            categoryCreateVO = new CategoryCreateVO(marketCategory2Select.getId()
                    , marketCategory2Select.getName()
                    , marketCategory2Select.getKeywords()
                    , marketCategory2Select.getDesc()
                    , marketCategory2Select.getPid()
                    , marketCategory2Select.getIconUrl()
                    , marketCategory2Select.getPicUrl()
                    , marketCategory2Select.getLevel()
                    , new Date()
                    , new Date());
        }
        return categoryCreateVO;
    }

    @Override
    public void categoryUpdate(CategoryDeleteBO categoryUpdateBO) {
        Integer id = categoryUpdateBO.getId();
        String name = categoryUpdateBO.getName();
        String keywords = categoryUpdateBO.getKeywords();
        String desc = categoryUpdateBO.getDesc();
        String iconUrl = categoryUpdateBO.getIconUrl();
        String picUrl = categoryUpdateBO.getPicUrl();
        String level = categoryUpdateBO.getLevel();
        List<CategoryDeleteBO.ChildrenDTO> childrens = categoryUpdateBO.getChildren();
        // 传入一个
        // 判断一级目录
        if ("L1".equals(level)) {
            updateCategory(id, name, keywords, desc, iconUrl, picUrl, level);
        }

        // 判断二级目录
        if ("L2".equals(level)) {
            updateCategory(id, name, keywords, desc, iconUrl, picUrl, level);
        }
    }

    /**
     * 更新 提取方法
     * 创建日期：2022/04/07 19:57
     *
     * @param id
     * @param name
     * @param keywords
     * @param desc
     * @param iconUrl
     * @param picUrl
     * @param level
     * @return void
     * @author rebaogu
     */
    private void updateCategory(Integer id, String name, String keywords, String desc, String iconUrl, String picUrl, String level) {
        MarketCategoryExample example = new MarketCategoryExample();

        MarketCategoryExample.Criteria criteria = example.createCriteria();

        criteria.andIdEqualTo(id);

        MarketCategory category = new MarketCategory(id, name, keywords, desc
                , null, iconUrl, picUrl, level, null, null, new Date(), null);

        marketCategoryMapper.updateByExampleSelective(category, example);
    }


    @Override
    public void categoryDelete(CategoryDeleteBO categoryDeleteBO) {
        String level = categoryDeleteBO.getLevel();

        MarketCategory category = new MarketCategory();
        category.setDeleted(true);

        // 判断一级目录
        if ("L1".equals(level)) {
            // 先删除二级目录,再删除一级目录
            // 获取一级目录id
            Integer id = categoryDeleteBO.getId();
            List<CategoryDeleteBO.ChildrenDTO> childrens = categoryDeleteBO.getChildren();
            for (CategoryDeleteBO.ChildrenDTO children : childrens) {
                // 获取二级目录id
                Integer idSecond = children.getId();

                MarketCategoryExample example = new MarketCategoryExample();

                MarketCategoryExample.Criteria criteria = example.createCriteria();

                criteria.andIdEqualTo(idSecond);


                // 更改deteid
                int count = marketCategoryMapper.updateByExampleSelective(category,example);
            }
            MarketCategoryExample exampleFirst = new MarketCategoryExample();

            MarketCategoryExample.Criteria criteria = exampleFirst.createCriteria();

            criteria.andIdEqualTo(id);
            //  删除一级目录
            marketCategoryMapper.deleteByExample(exampleFirst);
        }

        // 判断二级目录
        if ("L2".equals(level)) {
            Integer id = categoryDeleteBO.getId();
            MarketCategoryExample example = new MarketCategoryExample();

            MarketCategoryExample.Criteria criteria = example.createCriteria();

            criteria.andIdEqualTo(id);

            // 更改deteid
            int count = marketCategoryMapper.updateByExampleSelective(category,example);
        }

    }

    @Override
    public CategoryL1VO categoryL1() {
        // 查询类目名称列表 一个是value --id 一个是 label--名称

        MarketCategoryExample example = new MarketCategoryExample();
        MarketCategoryExample.Criteria criteria = example.createCriteria();
        criteria.andDeletedEqualTo(false);

        List<MarketCategory> marketCategories = marketCategoryMapper.selectByExample(example);


        List<CategoryL1InnerIdAndName> categoryL1InnerIdAndNames = new ArrayList<>();

        for (MarketCategory marketCategory : marketCategories) {
            CategoryL1InnerIdAndName categoryL1InnerIdAndName = new CategoryL1InnerIdAndName();
            categoryL1InnerIdAndName.setValue(marketCategory.getId());
            categoryL1InnerIdAndName.setLabel(marketCategory.getName());

            categoryL1InnerIdAndNames.add(categoryL1InnerIdAndName);
        }
        PageInfo<MarketCategory> pageInfo = new PageInfo<>();
        Integer total = Math.toIntExact(pageInfo.getTotal());
        int pages = pageInfo.getPages();

        return new CategoryL1VO(total, pages, total, pages, categoryL1InnerIdAndNames);
    }

}

